import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import { login } from './libs/interface'
import { userInfoSet, getUrlData } from './libs/publicMethod'
Vue.use(Router)

const router= new Router({
  // mode:'history',
  routes: [
    // 引导页面
    {
      path: '/',
      name: 'guide',
      component: ()=> import('./views/guide.vue'),
      meta: {
        title: '引导',
        keepAlive: false
      }
    },
    // 乐销商城
    {
      path: '/mall',
      name: 'mall',
      component: () => import('./views/mall.vue'),
      meta: {
        title: '乐销商城',
        keepAlive: false
      }
    },
    // 立刻赚钱
    {
      path: '/makeMoney',
      name: 'makeMoney',
      component: () => import('./views/makeMoney.vue'),
      meta: {
        title: '立刻赚钱',
        keepAlive: false
      }
    },
    // 发现
    {
      path: '/found/:id',
      name: 'found',
      component: () => import('./views/found.vue'),
      meta: {
        title: '发现',
        keepAlive: false
      }
    },
    // 游乐园
    {
      path: '/gamePark',
      name: 'gamePark',
      component: () => import('./views/gamePark.vue'),
      meta: {
        title: '游乐园',
        keepAlive: false
      }
    },
    // 我的
    {
      path: '/my',
      name: 'my',
      component: () => import('./views/my.vue'),
      meta: {
        title: '我的',
        keepAlive: false
      }
    },
    // 分类
    {
      path: '/category/:column/:id',
      name: 'category',
      component: () => import('./views/mall/category.vue'),
      meta: {
        title: '分类',
        keepAlive: false
      }
    },
    //商品详情
    {
      path: '/goods/:id',
      name: 'goods',
      component: () => import('./views/mall/goods.vue'),
      meta: {
        title: '商品',
        keepAlive: false
      }
    },
    // 购物车
    {
      path: '/shopCart',
      name: 'shopCart',
      component: () => import('./views/mall/shopCart.vue'),
      meta: {
        title: '购物车',
        keepAlive: false
      }
    },
    // 订单
    {
      path: '/order/:id',
      name: 'order',
      component: () => import('./views/mall/order.vue'),
      meta: {
        title: '订单',
        keepAlive: false
      }
    },
    // 购买
    {
      path: '/purchase/:id/:num',
      name: 'purchase',
      component: () => import('./views/mall/purchase.vue'),
      meta: {
        title: '购买',
        keepAlive: false
      }
    },
    // 爆款
    {
      path: '/hotCake/:column',
      name: 'hotCake',
      component: () => import('./views/mall/saleList.vue'),
      meta: {
        title: '爆款',
        keepAlive: false
        
      }
    },
    // 推荐
    {
      path: '/recommend/:column',
      name: 'recommend',
      component: () => import('./views/mall/saleList.vue'),
      meta: {
        title: '推荐',
        keepAlive: false
      }
    },
    // 限时抢购
    {
      path: '/flashSale/:column',
      name: 'flashSale',
      component: () => import('./views/mall/saleList.vue'),
      meta: {
        title: '限时抢购',
        keepAlive: false
      }
    },
    // 复消专区
    {
      path: '/sellAgain/:column',
      name: 'sellAgain',
      component: () => import('./views/mall/saleList.vue'),
      meta: {
        title: '复消专区',
        keepAlive: false
      }
    },
    // 众筹专区
    {
      path: '/crowdFunding/:id',
      name: 'crowdFunding',
      component: () => import('./views/mall/crowdFunding.vue'),
      meta: {
        title: '众筹专区',
        keepAlive: false
      }
    },
    // 膜力无限
    {
      path: '/facialMask',
      name: 'facialMask',
      component: () => import('./views/mall/facialMask.vue'),
      meta: {
        title: '好礼周周领',
        keepAlive: false
      }
    },
    // 领取记录
    {
      path: '/record',
      name: 'record',
      component: () => import('./views/mall/record.vue'),
      meta: {
        title: '领取记录',
        keepAlive: false
      }
    },
    // 领取记录
    {
      path: '/receiveGoodses',
      name: 'receiveGoodses',
      component: () => import('./views/mall/receiveGoodses.vue'),
      meta: {
        title: '领取商品',
        keepAlive: false
      }
    },
    // 商品详情
    {
      path: '/receiveGoods/:id',
      name: 'receiveGoods',
      component: () => import('./views/mall/receiveGoods.vue'),
      meta: {
        title: '商品详情',
        keepAlive: false
      }
    },
    // 面膜支付
    {
      path: '/faciaMaskPay/:id',
      name: 'faciaMaskPay',
      component: () => import('./views/mall/faciaMaskPay.vue'),
      meta: {
        title: '面膜支付',
        keepAlive: false
      }
    },
    // 众筹详情
    {
      path: '/crowdFundingGoods/:join/:joinPeople/:crowdFundingId/:id',
      name: 'crowdFundingGoods',
      component: () => import('./views/mall/crowdFundingGoods.vue'),
      meta: {
        title: '众筹详情',
        keepAlive: false
      }
    },
    // 众筹支付
    {
      path: '/crowdFundingPay/:join/:crowdFundingId/:id' ,
      name: 'crowdFundingPay',
      component: () => import('./views/mall/crowdFundingPay.vue'),
      meta: {
        title: '众筹支付',
        keepAlive: false
      }
    },
    // 打卡日历
    {
      path: '/calendar/:id',
      name: 'calendar',
      component: () => import('./views/mall/calendar.vue'),
      meta: {
        title: '打卡日历',
        keepAlive: false
      }
    },
    // 活动专区
    {
      path: '/activity',
      name: 'activity',
      component: () => import('./views/mall/activity.vue'),
      meta: {
        title: '活动专区',
        keepAlive: false
      }
    },
    // 即买即送
    {
      path: '/jmjs/:id',
      name: 'jmjs',
      component: () => import('./views/mall/jmjs.vue'),
      meta: {
        title: '即买即送',
        keepAlive: false
      }
    },
    // 商品详情
    {
      path: '/jmjsGoods/:id',
      name: 'jmjsGoods',
      component: () => import('./views/mall/jmjsGoods.vue'),
      meta: {
        title: '商品详情',
        keepAlive: false
      }
    },
    // 打卡送礼支付
    {
      path: '/jmjsPay/:skuId/:num',
      name: 'jmjsPay',
      component: () => import('./views/mall/jmjsPay.vue'),
      meta: {
        title: '打卡送礼',
        keepAlive: false
      }
    },
    // 收货地址
    {
      path: '/deliveryAddress/:isSelected',
      name: 'deliveryAddress',
      component: () => import('./views/my/deliveryAddress.vue'),
      meta: {
        title: '收货地址',
        keepAlive: false
      }
    },
    // 我的卡券
    {
      path: '/coupon',
      name: 'coupon',
      component: () => import('./views/my/coupon.vue'),
      meta: {
        title: '我的卡券',
        keepAlive: false
      }
    },
    // 众筹卡券
    {
      path: '/discount/:id',
      name: 'discount',
      component: () => import('./views/my/discount.vue'),
      meta: {
        title: '卡券',
        keepAlive: false
      }
    },
    // 我的乐豆
    {
      path: '/ledou',
      name: 'ledou',
      component:() => import('./views/my/ledou.vue'),
      meta: {
        title: '我的乐豆',
        keepAlive:false
      }
    },
    // 我的积分
    {
      path: '/points',
      name: 'points',
      component: () => import('./views/my/points.vue'),
      meta: {
        title: '我的积分',
        keepAlive:false
      }
    },
    // 乐销基金
    {
      path: '/lxFund',
      name: 'lxFund',
      component:() => import('./views/my/lxFund.vue'),
      meta: {
        title: '乐销基金',
        keepAlive:false
      }
    },
    // 我的订单
    {
      path: '/myOrder',
      name: 'myOrder',
      component:() => import('./views/my/myOrder.vue'),
      meta: {
        title: '我的订单',
        keepAlive:false
      }
    },
    // 订单详情页
    {
      path: '/orderDetail/:id',
      name: 'orderDetail',
      component: () => import('./views/my/orderDetail.vue'),
      meta: {
        title: '订单详情页',
        keepAlive:false
      }
    },
    // 购物清单
    {
      path: '/shoppingList',
      name: 'shoppingList',
      component:() => import('./views/my/shoppingList.vue'),
      meta: {
        title: '购物清单',
        keepAlive:false
      }
    },
    // 收藏
    {
      path: '/collection',
      name: 'collection',
      component:() => import('./views/my/collection.vue'),
      meta: {
        title: '收藏',
        keepAlive:false
      }
    },
    // 关于我们
    {
      path: '/aboutUs',
      name: 'aboutUs',
      component: () => import('./views/my/aboutUs.vue'),
      meta: {
        title: '关于我们',
        keepAlive:false
      }
    },
    // 用户协议
    {
      path: '/userAgreement',
      name: 'userAgreement',
      component: () => import('./views/my/userAgreement.vue'),
      meta: {
        title: '用户协议',
        keepAlive:false
      }
    },
    // 足迹
    {
      path: '/myFootPrint',
      name: 'myFootPrint',
      component:() => import('./views/my/myFootPrint.vue'),
      meta: {
        title: '足迹',
        keepAlive:false
      }
    },
    {
      path:'/rankList/:id',
      name: 'rankList',
      component:()=>import('./views/my/rankList.vue'),
      meta: {
        title: '排行榜',
        keepAlive: false
      }
    },
    // 充值
    {
      path: '/payview/:num',
      name: 'payview',
      component:() => import('./views/other/payview.vue'),
      meta: {
        title: '充值',
        keepAlive:false
      }
    },
    // 分享
    {
      path: '/wxshare',
      name: 'wxshare',
      component:() => import('./views/other/wxshare.vue'),
      meta: {
        title: '充值',
        keepAlive:false
      }
    },
    // 乐销商学院
    {
      path: '/lexiaoDetail/:id',
      name: 'lexiaoDetail',
      component: () => import('./views/other/lexiaoDetail.vue'),
      meta: {
        title: '乐销商学院',
        keepAlive:false
      }
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      meta: {
        title: 'home',
        keepAlive: false
      }
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ './views/About.vue'),
      meta: {
        title: 'about',
        keepAlive: false
      }
    }
  ]
})
export default router
router.beforeEach(({meta}, from, next) => {
  console.log(from)
  document.title = meta.title
  if(!sessionStorage.getItem('historyUrl')){
    sessionStorage.setItem('historyUrl',getUrlData('gotoPage'))
  }
  if(!sessionStorage.getItem('webUrl')){
    sessionStorage.setItem('webUrl',window.location.href)
  }
  if(!sessionStorage.getItem('userInfo')) {
    try{
      let params = {
        wxCode: getUrlData('code'),
        memberID: 0,
        inviterID: sessionStorage.getItem('inviterID') || getUrlData('state').split('#')[0]
      }
      login(params, (data)=>{
        if (data.code == 1) {
          userInfoSet(data.payload); // 把用户信息加入session        
          location.replace('http://www.xiewan8.com/lyApp')
        }else{
          window.location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx163c4c3d188f8ee5&redirect_uri=http%3A%2F%2Fwww.xiewan8.com%2FlyApp%2F%23%2Fmy&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect'
        }
      })
    }
    catch(err){
      console.log(err)
      window.location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx163c4c3d188f8ee5&redirect_uri=http%3A%2F%2Fwww.xiewan8.com%2FlyApp%2F%23%2Fmy&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect'
    }

    // try {
    //   console.log(getUrlData('inviterID'));
    //   // userInfoSet({"token":"091d1167e9735ba93aaaeb95ddfcb49c","member":{"id":2743,"channelLv":1,"sex":null,"name":null,"nick":"恩周","phone":null,"inviter":2743,"password":null,"state":1,"photo":"http://th irdwx.qlogo.cn/mmopen/HdUhofEFKPriaibOoYZXq8mKOWh5GtVFZYNG9Q6rgBHa1sTN6ibo5aHrw1B2jLERqwTq2icrbO0YQ7xbwop6ocD8zVbibEH4GfZTH/64","birthday":null,"channelid":283,"idcard":null,"createtime":1528720507000,"wfCode":null,"age":0,"addressList":null,"subscribe":1,"lastlogintime":1537169370110,"type":2,"wxUnionID":"oX_3CwivKkDbKVx9VZ6qCvQa6RTg"}})
    //   let params = {
    //     wxCode: '021CJAOx1j4sg90UXERx1imrOx1CJAO1',
    //     memberID: 0,
    //     inviterID: -1
    //   }
    //   login(params, (data)=>{
    //     if (data.code == 1) {
    //       userInfoSet(data.payload); // 把用户信息加入session        
    //       location.replace('http://localhost:9527/#/')
    //     } else {
    //       window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx163c4c3d188f8ee5&redirect_uri=http%3A%2F%2Fwww.xiewan8.com%2FlyApp%2F%23%2Fmy&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect'
    //     }
    //   })
    // }
    // catch(err){
    //   console.log(err)
    //   window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx163c4c3d188f8ee5&redirect_uri=http%3A%2F%2Fwww.xiewan8.com%2FlyApp%2F%23%2Fmy&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect'
    // }
  }
  next()
})
